//
//  OverlapIconsView.swift
//  SCA
//
//  Created by tjun on 2025/9/16.
//

import UIKit

//class OverlapIconsView: UIView {
//    
//    private var imageViews: [UIImageView] = []
//    
//    /// 设置图标
//    /// - Parameters:
//    ///   - images: 图片数组
//    ///   - size: 每个图标大小
//    ///   - overlap: 重叠比例（0~1，越大重叠越多）
//    func configure(imagesUrls: [String], size: CGFloat = 30, overlap: CGFloat = 0.3) {
//        // 清空旧的
//        imageViews.forEach { $0.removeFromSuperview() }
//        imageViews.removeAll()
//        
//        for (index, url) in imagesUrls.enumerated() {
//            let imageView = UIImageView()
//            imageView.setImage(url: url)
//            imageView.contentMode = .scaleAspectFill
//            imageView.layer.cornerRadius = size / 2
//            imageView.clipsToBounds = true
//            imageView.layer.borderWidth = 2
//            imageView.layer.borderColor = UIColor.black.withAlphaComponent(0.2).cgColor
//            
//            addSubview(imageView)
//            imageViews.append(imageView)
//            
//            // 计算位置
//            let x = CGFloat(index) * size * (1 - overlap)
//            imageView.frame = CGRect(x: x, y: 0, width: size, height: size)
//            
//            // 控制层级：后面的在前面
//            bringSubviewToFront(imageView)
//        }
//        
//        // 更新自身大小
//        let totalWidth = CGFloat(imagesUrls.count) * size * (1 - overlap) + size * overlap
//        self.frame.size = CGSize(width: totalWidth, height: size)
//    }
//}


import UIKit
import SnapKit

class OverlapIconsView: UIView {
    
    private var imageViews: [UIImageView] = []
    private var imageSize: CGFloat = 30
    private var overlapRatio: CGFloat = 0.3
    
    /// 设置图标
    /// - Parameters:
    ///   - imagesUrls: 图片 URL 数组
    ///   - size: 每个图标大小
    ///   - overlap: 重叠比例（0~1，越大重叠越多）
    func configure(imagesUrls: [String], size: CGFloat = 30, overlap: CGFloat = 0.3) {
        // 清空旧的
        imageViews.forEach { $0.removeFromSuperview() }
        imageViews.removeAll()
        
        self.imageSize = size
        self.overlapRatio = overlap
        
        var previousView: UIView? = nil
        
        for (index, url) in imagesUrls.enumerated() {
            let imageView = UIImageView()
            imageView.setImage(url: url) // 你已有的扩展方法
            imageView.contentMode = .scaleAspectFill
            imageView.layer.cornerRadius = size / 2
            imageView.clipsToBounds = true
            imageView.layer.borderWidth = 2
            imageView.layer.borderColor = UIColor.black.withAlphaComponent(0.2).cgColor
            
            addSubview(imageView)
            imageViews.append(imageView)
            
            imageView.snp.makeConstraints { make in
                make.top.bottom.equalToSuperview()
                make.width.height.equalTo(size)
                
                if let prev = previousView {
                    make.leading.equalTo(prev.snp.leading).offset(size * (1 - overlap))
                } else {
                    make.leading.equalToSuperview()
                }
            }
            
            // 控制层级：后面的在前面
            bringSubviewToFront(imageView)
            previousView = imageView
        }
        
        // 最后一个约束到父视图尾部
        if let last = previousView {
            last.snp.makeConstraints { make in
                make.trailing.equalToSuperview()
            }
        }
    }
}
